package milkproduction.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import milkproduction.to.OrderTO;
import miracom.one.MilkProduction.util.DatabaseUtility;

public class OrderDAO {
	private DatabaseUtility util;
	private static OrderDAO instance = new OrderDAO();;
	private OrderDAO(){
		util = DatabaseUtility.getInstance();				
	}
	public static OrderDAO getInstacne(){
		return instance;
	}
	
	public OrderTO selectOrder()throws SQLException{
		String sql = "select order_no, to_char(order_date, 'YYYY-MM-DD'), orderer, quantity, delivery_state from orders where order_no = (select max(order_no) from orders)";
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rset = null;
		OrderTO order=null;
		try{
			conn = util.getDataSource().getConnection();
			pstmt = conn.prepareStatement(sql);
			rset = pstmt.executeQuery();
			if(rset.next()){
				if(rset.getString(5).equals("true")){
					order = new OrderTO(rset.getInt(1), rset.getString(2), rset.getString(3), rset.getInt(4), true);
				}else{
					order = new OrderTO(rset.getInt(1), rset.getString(2), rset.getString(3), rset.getInt(4), false);
	
				}
			}
		}finally{
			util.close(rset, pstmt, conn);
		}
		return order;
	}

}
